//
//  ANManageTableViewController.m
//  Ants
//
//  Created by ZHM on 16/6/27.
//  Copyright © 2016年 ZHM. All rights reserved.
//

#import "ANManageTableViewController.h"
#import "ANFindTool.h"

@interface ANManageTableViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, assign) CGFloat beforeY;

@property (nonatomic, strong) ANFindTool *barTool;
@property (nonatomic, weak) UIView *taskbarViewBg;
@property (nonatomic, weak) UITableView *tableView;

@end

@implementation ANManageTableViewController



- (instancetype)init {
    if (self = [super init]){
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}

- (void)dealloc {
    [_barTool removeFromSuperview];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    ANLog(@"管理界面消失");
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    //取消隐藏效果
    [_barTool recoveryBar];
    [_taskbarViewBg removeFromSuperview];
}
- (void)loadView {
    [super loadView];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    //任务栏颜色
    UIView *taskbarViewBg = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ANKeyWindow.width, 20)];
    taskbarViewBg.backgroundColor = [UIColor colorWithRed:246.0/255 green:246.0/255 blue:249.0/255 alpha:1];
    _taskbarViewBg = taskbarViewBg;
    [ANKeyWindow addSubview:taskbarViewBg];
    
    
    //设置导航条
    [self setUpNav];
    //ios8 自带的隐藏功能
    //self.navigationController.hidesBarsOnSwipe = YES;
    
    CGRect tableViewFrame = CGRectMake(0, 35, ANKeyWindow.width, ANKeyWindow.height);
    UITableView *tableView = [[UITableView alloc] initWithFrame:tableViewFrame style:UITableViewStylePlain];
    _tableView = tableView;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    //工具条
    _barTool = [ANFindTool showWithTableView:_tableView];
    
    [self.view addSubview:tableView];
    [self.view addSubview:_barTool];
}

- (void)setUpNav {
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"管理商品";
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"添加" style:UIBarButtonItemStylePlain target:self action:@selector(rightItemCilck)];
    self.navigationItem.rightBarButtonItem = rightItem;
    
}
- (void)rightItemCilck {
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (_beforeY > scrollView.contentOffset.y  && _barTool.barIsHidden && scrollView.contentOffset.y < scrollView.contentSize.height - ANKeyWindow.height) {
        [_barTool recoveryBar];
    }
    if (_beforeY < scrollView.contentOffset.y && !_barTool.barIsHidden && scrollView.contentOffset.y > -64) {
        [_barTool hiddenBar];
    }
    _beforeY = scrollView.contentOffset.y;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 160;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ID = @"MANAGE_CELL";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"%ld",(long)indexPath.row];
    return cell;
}



@end
